#!/usr/bin/env python

import os
import sys
from optparse import HelpFormatter

sys.path.append(os.path.abspath('static/'))

import upload


class GCWikiHelpFormatter (HelpFormatter):
    """Format help with wiki markup for Google Code."""

    def __init__(self,
                 indent_increment=2,
                 max_help_position=24,
                 width=None,
                 short_first=1):
        HelpFormatter.__init__(
            self, indent_increment, max_help_position, width, short_first)
        self._dl_open = False

    def indent(self):
        self._pending = 'INDENT'
        HelpFormatter.indent(self)

    def dedent(self):
        self._pending = 'DEDENT'
        HelpFormatter.dedent(self)

    def format_usage(self, usage):
        return "*Usage summary:* `%s`\n" % usage

    def format_heading(self, heading):
        if self._dl_open:
            pre = '\n</dl>\n'
        else:
            pre = ''
        markup = '='*(self.current_indent+2)
        self._dl_open = True
        return "%s%s %s %s\n<dl>\n" % (pre, markup, heading, markup)

    def format_option(self, option):
        result = []
        opts = self.option_strings[option]
        result.append('<dt>`%s`</dt>\n' % opts)
        if option.help:
            help_text = '<dd>%s</dd>\n' % self.expand_default(option)
            result.append(help_text)
        return ''.join(result)


def main():
    upload.parser.formatter = GCWikiHelpFormatter()
    print HEADER
    print upload.parser.format_option_help()
    print '</dl>'  # TODO: Formatter should do this
    print

HEADER = """#summary upload.py usage and options.
<wiki:comment>
THIS PAGE IS AUTOGENERATED. DO NOT EDIT.
To update this page run tools/uploadopts2wiki.py
</wiki:comment>

= upload.py Usage =

[http://codereview.appspot.com/static/upload.py upload.py] is a tool
for uploading diffs from a version control system to the codereview app.

*Usage summary:*

{{{upload.py [options] [-- diff_options]}}}

Diff options are passed to the diff command of the underlying system.

*Supported version control systems:*
 * Git
 * Mercurial
 * Subversion

It is important for Git/Mercurial users to specify a tree/node/branch to diff
against by using the '--rev' option.

"""


if __name__ == '__main__':
    main()
